import 'dart:io';

import 'package:dio/dio.dart';
import 'package:http_parser/http_parser.dart';
import 'package:retrofit/retrofit.dart';
import 'package:school_assistant/basedio.dart';

import 'data/base_data_entity.dart';
import 'data/login_res_entity.dart';

part 'common_client.g.dart';

@RestApi()
abstract class CommonClient {
  factory CommonClient({Dio? dio, String? baseUrl}) {
    dio = BaseDio.getInstance()!.getDio();
    return _CommonClient(dio, baseUrl: baseUrl);
  }

  @POST("/article")
  Future<BaseDataEntity> postArticle(
      @Field('user') String user,
      @Field('ty') int type,
      @Field('title') String title,
      @Field('body') String body,
      @Field('image') String image,
      {@Field('extra') String? extra});

  @PATCH("/article")
  Future<BaseDataEntity> updateArticle(
      @Field('id') String id,
      @Field('user') String user,
      @Field('ty') int ty,
      @Field('title') String title,
      @Field('body') String body,
      @Field('image') String image,
      @Field('extra') String extra);

  @POST("/user")
  Future<LoginResEntity> register(
      @Field('email') String email,
      @Field('password') String password,
      @Field('username') String username,
      @Field('mobile') String mobile,
      @Field('profile') String profile,
      @Field('sex') int sex,
      @Field('age') int age,
      {@Field('extra') String? extra});

  @PATCH("/user")
  Future<BaseDataEntity> updateUser(
      @Field('id') String id,
      @Field('email') String email,
      @Field('password') String password,
      @Field('username') String username,
      @Field('mobile') String mobile,
      @Field('profile') String profile,
      @Field('sex') int sex,
      @Field('age') int age,
      {@Field('extra') String? extra});

  @GET("/user/{id}")
  Future<BaseDataEntity> getUser(
    @Path('id') String id,
  );

  @PUT("/user")
  Future<LoginResEntity> login(
    @Field('email') String email,
    @Field('password') String password,
  );

  @GET("/article/{page}/{size}")
  Future<BaseDataEntity> getArticleAll(
    @Path('page') int page,
    @Path('size') int size,
  );

  @GET("/article/{page}/{size}/{user}")
  Future<BaseDataEntity> getArticle(
    @Path('page') int page,
    @Path('size') int size, {
    @Path('user') String? user,
  });

  @POST("/product")
  Future<BaseDataEntity> addGood(
      @Field('category') int category,
      @Field('name') String name,
      @Field('price') double price,
      @Field('image') String image);

  @PATCH("/product")
  Future<BaseDataEntity> updateGood(
      @Field('category') int category,
      @Field('name') String name,
      @Field('price') double price,
      @Field('image') String image);

  @GET("/product/{page}/{size}")
  Future<BaseDataEntity> getGoods(
    @Path('page') int page,
    @Path('size') int size,
  );

  @POST("/order")
  Future<BaseDataEntity> createOrder(
    @Field('user') String user,
    @Field('product') String product, {
    @Field('extra') String? extra,
  });

  @POST("/like")
  Future<BaseDataEntity> like(
    @Field('user') String user,
    @Field('article') String article,
  );

  @DELETE("/like")
  Future<BaseDataEntity> unlike(
    @Field('user') String user,
    @Field('article') String article,
  );

  @GET("/order/{user}")
  Future<BaseDataEntity> getOrders(
    @Path('user') String user,
  );

  @POST("/upload")
  @MultiPart()
  Future<BaseDataEntity> commonUpFile(
      @Part(contentType: 'image/jpeg') File file);
}
